﻿using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

namespace ZSZ.EntityFramework.Migrations
{
    public partial class zszMigration : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "T_Attachments",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreateDateTime = table.Column<DateTime>(nullable: false),
                    IsDeleted = table.Column<bool>(nullable: false),
                    Name = table.Column<string>(maxLength: 50, nullable: false),
                    IconName = table.Column<string>(unicode: false, maxLength: 50, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_Attachments", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "T_City",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreateDateTime = table.Column<DateTime>(nullable: false),
                    IsDeleted = table.Column<bool>(nullable: false),
                    Name = table.Column<string>(maxLength: 20, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_City", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "T_IdNames",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreateDateTime = table.Column<DateTime>(nullable: false),
                    IsDeleted = table.Column<bool>(nullable: false),
                    TypeName = table.Column<string>(maxLength: 1024, nullable: false),
                    Name = table.Column<string>(maxLength: 1024, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_IdNames", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "T_Permissions",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreateDateTime = table.Column<DateTime>(nullable: false),
                    IsDeleted = table.Column<bool>(nullable: false),
                    Description = table.Column<string>(maxLength: 1024, nullable: true),
                    Name = table.Column<string>(maxLength: 50, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_Permissions", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "T_Roles",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreateDateTime = table.Column<DateTime>(nullable: false),
                    IsDeleted = table.Column<bool>(nullable: false),
                    Name = table.Column<string>(maxLength: 50, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_Roles", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "T_Settings",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreateDateTime = table.Column<DateTime>(nullable: false),
                    IsDeleted = table.Column<bool>(nullable: false),
                    Name = table.Column<string>(maxLength: 1024, nullable: false),
                    Value = table.Column<string>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_Settings", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "T_AdminUsers",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreateDateTime = table.Column<DateTime>(nullable: false),
                    IsDeleted = table.Column<bool>(nullable: false),
                    Name = table.Column<string>(maxLength: 50, nullable: false),
                    PhoneNum = table.Column<string>(unicode: false, maxLength: 20, nullable: false),
                    PasswordHash = table.Column<string>(unicode: false, maxLength: 100, nullable: false),
                    PasswordSalt = table.Column<string>(unicode: false, maxLength: 20, nullable: false),
                    Email = table.Column<string>(unicode: false, maxLength: 30, nullable: false),
                    CityId = table.Column<long>(nullable: true),
                    LoginErrorTimes = table.Column<int>(nullable: false),
                    LastLoginErrorDateTime = table.Column<DateTime>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_AdminUsers", x => x.Id);
                    table.ForeignKey(
                        name: "FK_T_AdminUsers_T_City_CityId",
                        column: x => x.CityId,
                        principalTable: "T_City",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "T_Regions",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreateDateTime = table.Column<DateTime>(nullable: false),
                    IsDeleted = table.Column<bool>(nullable: false),
                    Name = table.Column<string>(maxLength: 50, nullable: false),
                    CityId = table.Column<long>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_Regions", x => x.Id);
                    table.ForeignKey(
                        name: "FK_T_Regions_T_City_CityId",
                        column: x => x.CityId,
                        principalTable: "T_City",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "T_Users",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreateDateTime = table.Column<DateTime>(nullable: false),
                    IsDeleted = table.Column<bool>(nullable: false),
                    PhoneNum = table.Column<string>(unicode: false, maxLength: 20, nullable: false),
                    PasswordHash = table.Column<string>(maxLength: 100, nullable: false),
                    PasswordSalt = table.Column<string>(maxLength: 20, nullable: false),
                    LoginErrorTimes = table.Column<int>(nullable: false),
                    LastLoginErrorDateTime = table.Column<DateTime>(nullable: true),
                    CityId = table.Column<long>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_Users", x => x.Id);
                    table.ForeignKey(
                        name: "FK_T_Users_T_City_CityId",
                        column: x => x.CityId,
                        principalTable: "T_City",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "T_RolePermission",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreateDateTime = table.Column<DateTime>(nullable: false),
                    IsDeleted = table.Column<bool>(nullable: false),
                    RoleId = table.Column<long>(nullable: false),
                    PermissionId = table.Column<long>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_RolePermission", x => x.Id);
                    table.ForeignKey(
                        name: "FK_T_RolePermission_T_Permissions_PermissionId",
                        column: x => x.PermissionId,
                        principalTable: "T_Permissions",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_T_RolePermission_T_Roles_RoleId",
                        column: x => x.RoleId,
                        principalTable: "T_Roles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "T_AdminLogs",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreateDateTime = table.Column<DateTime>(nullable: false),
                    IsDeleted = table.Column<bool>(nullable: false),
                    AdminUserId = table.Column<long>(nullable: false),
                    Message = table.Column<string>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_AdminLogs", x => x.Id);
                    table.ForeignKey(
                        name: "FK_T_AdminLogs_T_AdminUsers_AdminUserId",
                        column: x => x.AdminUserId,
                        principalTable: "T_AdminUsers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "T_AdminUserRole",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreateDateTime = table.Column<DateTime>(nullable: false),
                    IsDeleted = table.Column<bool>(nullable: false),
                    AdminUserId = table.Column<long>(nullable: false),
                    RoleId = table.Column<long>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_AdminUserRole", x => x.Id);
                    table.ForeignKey(
                        name: "FK_T_AdminUserRole_T_AdminUsers_AdminUserId",
                        column: x => x.AdminUserId,
                        principalTable: "T_AdminUsers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_T_AdminUserRole_T_Roles_RoleId",
                        column: x => x.RoleId,
                        principalTable: "T_Roles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "T_Community",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreateDateTime = table.Column<DateTime>(nullable: false),
                    IsDeleted = table.Column<bool>(nullable: false),
                    Name = table.Column<string>(maxLength: 20, nullable: false),
                    RegionId = table.Column<long>(nullable: false),
                    Location = table.Column<string>(maxLength: 1024, nullable: false),
                    Traffic = table.Column<string>(nullable: true),
                    BuiltYear = table.Column<int>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_Community", x => x.Id);
                    table.ForeignKey(
                        name: "FK_T_Community_T_Regions_RegionId",
                        column: x => x.RegionId,
                        principalTable: "T_Regions",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "T_House",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreateDateTime = table.Column<DateTime>(nullable: false),
                    IsDeleted = table.Column<bool>(nullable: false),
                    CommunityId = table.Column<long>(nullable: false),
                    RoomTypeId = table.Column<long>(nullable: false),
                    Address = table.Column<string>(maxLength: 128, nullable: false),
                    MonthRent = table.Column<int>(nullable: false),
                    StatusId = table.Column<long>(nullable: false),
                    Area = table.Column<decimal>(nullable: false),
                    DecorateStatusId = table.Column<long>(nullable: false),
                    TotalFloorCount = table.Column<int>(nullable: false),
                    FloorIndex = table.Column<int>(nullable: false),
                    TypeId = table.Column<long>(nullable: false),
                    Direction = table.Column<string>(maxLength: 20, nullable: false),
                    LookableDateTime = table.Column<DateTime>(nullable: false),
                    CheckInDateTime = table.Column<DateTime>(nullable: false),
                    OwnerName = table.Column<string>(maxLength: 20, nullable: false),
                    OwnerPhoneNum = table.Column<string>(unicode: false, maxLength: 20, nullable: false),
                    Description = table.Column<string>(maxLength: 500, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_House", x => x.Id);
                    table.ForeignKey(
                        name: "FK_T_House_T_Community_CommunityId",
                        column: x => x.CommunityId,
                        principalTable: "T_Community",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_T_House_T_IdNames_DecorateStatusId",
                        column: x => x.DecorateStatusId,
                        principalTable: "T_IdNames",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_T_House_T_IdNames_RoomTypeId",
                        column: x => x.RoomTypeId,
                        principalTable: "T_IdNames",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_T_House_T_IdNames_StatusId",
                        column: x => x.StatusId,
                        principalTable: "T_IdNames",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_T_House_T_IdNames_TypeId",
                        column: x => x.TypeId,
                        principalTable: "T_IdNames",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "T_HouseAppointment",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreateDateTime = table.Column<DateTime>(nullable: false),
                    IsDeleted = table.Column<bool>(nullable: false),
                    UserId = table.Column<long>(nullable: true),
                    Name = table.Column<string>(maxLength: 20, nullable: false),
                    PhoneNum = table.Column<string>(unicode: false, maxLength: 20, nullable: false),
                    VisitDate = table.Column<DateTime>(nullable: false),
                    HouseId = table.Column<long>(nullable: false),
                    Status = table.Column<string>(maxLength: 20, nullable: false),
                    FollowAdminUserId = table.Column<long>(nullable: true),
                    FollowDateTime = table.Column<DateTime>(nullable: true),
                    RowVersion = table.Column<DateTime>(rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_HouseAppointment", x => x.Id);
                    table.ForeignKey(
                        name: "FK_T_HouseAppointment_T_AdminUsers_FollowAdminUserId",
                        column: x => x.FollowAdminUserId,
                        principalTable: "T_AdminUsers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_T_HouseAppointment_T_House_HouseId",
                        column: x => x.HouseId,
                        principalTable: "T_House",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_T_HouseAppointment_T_Users_UserId",
                        column: x => x.UserId,
                        principalTable: "T_Users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "T_HouseAttachment",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreateDateTime = table.Column<DateTime>(nullable: false),
                    IsDeleted = table.Column<bool>(nullable: false),
                    HouseId = table.Column<long>(nullable: false),
                    AttachmentId = table.Column<long>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_HouseAttachment", x => x.Id);
                    table.ForeignKey(
                        name: "FK_T_HouseAttachment_T_Attachments_AttachmentId",
                        column: x => x.AttachmentId,
                        principalTable: "T_Attachments",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_T_HouseAttachment_T_House_HouseId",
                        column: x => x.HouseId,
                        principalTable: "T_House",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "T_HousePic",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreateDateTime = table.Column<DateTime>(nullable: false),
                    IsDeleted = table.Column<bool>(nullable: false),
                    HouseId = table.Column<long>(nullable: false),
                    Url = table.Column<string>(maxLength: 1024, nullable: false),
                    ThumbUrl = table.Column<string>(maxLength: 1024, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_HousePic", x => x.Id);
                    table.ForeignKey(
                        name: "FK_T_HousePic_T_House_HouseId",
                        column: x => x.HouseId,
                        principalTable: "T_House",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_T_AdminLogs_AdminUserId",
                table: "T_AdminLogs",
                column: "AdminUserId");

            migrationBuilder.CreateIndex(
                name: "IX_T_AdminUserRole_AdminUserId",
                table: "T_AdminUserRole",
                column: "AdminUserId");

            migrationBuilder.CreateIndex(
                name: "IX_T_AdminUserRole_RoleId",
                table: "T_AdminUserRole",
                column: "RoleId");

            migrationBuilder.CreateIndex(
                name: "IX_T_AdminUsers_CityId",
                table: "T_AdminUsers",
                column: "CityId");

            migrationBuilder.CreateIndex(
                name: "IX_T_Community_RegionId",
                table: "T_Community",
                column: "RegionId");

            migrationBuilder.CreateIndex(
                name: "IX_T_House_CommunityId",
                table: "T_House",
                column: "CommunityId");

            migrationBuilder.CreateIndex(
                name: "IX_T_House_DecorateStatusId",
                table: "T_House",
                column: "DecorateStatusId");

            migrationBuilder.CreateIndex(
                name: "IX_T_House_RoomTypeId",
                table: "T_House",
                column: "RoomTypeId");

            migrationBuilder.CreateIndex(
                name: "IX_T_House_StatusId",
                table: "T_House",
                column: "StatusId");

            migrationBuilder.CreateIndex(
                name: "IX_T_House_TypeId",
                table: "T_House",
                column: "TypeId");

            migrationBuilder.CreateIndex(
                name: "IX_T_HouseAppointment_FollowAdminUserId",
                table: "T_HouseAppointment",
                column: "FollowAdminUserId");

            migrationBuilder.CreateIndex(
                name: "IX_T_HouseAppointment_HouseId",
                table: "T_HouseAppointment",
                column: "HouseId");

            migrationBuilder.CreateIndex(
                name: "IX_T_HouseAppointment_UserId",
                table: "T_HouseAppointment",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_T_HouseAttachment_AttachmentId",
                table: "T_HouseAttachment",
                column: "AttachmentId");

            migrationBuilder.CreateIndex(
                name: "IX_T_HouseAttachment_HouseId",
                table: "T_HouseAttachment",
                column: "HouseId");

            migrationBuilder.CreateIndex(
                name: "IX_T_HousePic_HouseId",
                table: "T_HousePic",
                column: "HouseId");

            migrationBuilder.CreateIndex(
                name: "IX_T_Regions_CityId",
                table: "T_Regions",
                column: "CityId");

            migrationBuilder.CreateIndex(
                name: "IX_T_RolePermission_PermissionId",
                table: "T_RolePermission",
                column: "PermissionId");

            migrationBuilder.CreateIndex(
                name: "IX_T_RolePermission_RoleId",
                table: "T_RolePermission",
                column: "RoleId");

            migrationBuilder.CreateIndex(
                name: "IX_T_Users_CityId",
                table: "T_Users",
                column: "CityId");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "T_AdminLogs");

            migrationBuilder.DropTable(
                name: "T_AdminUserRole");

            migrationBuilder.DropTable(
                name: "T_HouseAppointment");

            migrationBuilder.DropTable(
                name: "T_HouseAttachment");

            migrationBuilder.DropTable(
                name: "T_HousePic");

            migrationBuilder.DropTable(
                name: "T_RolePermission");

            migrationBuilder.DropTable(
                name: "T_Settings");

            migrationBuilder.DropTable(
                name: "T_AdminUsers");

            migrationBuilder.DropTable(
                name: "T_Users");

            migrationBuilder.DropTable(
                name: "T_Attachments");

            migrationBuilder.DropTable(
                name: "T_House");

            migrationBuilder.DropTable(
                name: "T_Permissions");

            migrationBuilder.DropTable(
                name: "T_Roles");

            migrationBuilder.DropTable(
                name: "T_Community");

            migrationBuilder.DropTable(
                name: "T_IdNames");

            migrationBuilder.DropTable(
                name: "T_Regions");

            migrationBuilder.DropTable(
                name: "T_City");
        }
    }
}
